package com.surfilter.msmp.common.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;

/**
 * 描述：
 * 作者: JinHuaTao
 * 时间：2017/6/30 14:04
 */
public class Base64Util {

    /**
     * 解密字符串
     * @param cryptStr
     * */
    public static String decodeBase64(String cryptStr){
        byte[] bytes = Base64.decodeBase64(cryptStr);
        return new String(bytes);
    }

    /**
     * 加密字符串
     * @param source
     * */
    public static String encodeBase64(final String source){
        byte b[] = source.getBytes();
        byte[] results = Base64.encodeBase64(b);
        return new String(results);
    }

    public static String encodeFile(String filePath) throws IOException {
        InputStream reader = Base64Util.class.getResourceAsStream(filePath);
        byte b[] = IOUtils.toByteArray(reader);
        byte[] results = Base64.encodeBase64(b);
        return new String(results);
    }


}
